package controllers;

import dao.DocumentTypeDAO;
import dao.FranchiseDAO;
import dao.GeneralDAO;
import dao.TraceStateDAO;
import entities.*;
import facade.facadeClass;
import java.sql.Date;

/**
 * @author Sergio Perez 257605
 */
public class SyntacticValidator {

    private GeneralDAO<Trace> traceDAO;
    private DocumentTypeDAO documentTypeDAO;
    private TraceStateDAO traceStateDao;
    private FranchiseDAO franchiseDAO;
    private Trace entityTrace;
    private Archive entityArchive;
    private DocumentType entityDocumentType;
    private Franchise entityFranchise;
    private TraceState entityTraceState;
    /**
     * valida la traza
     *
     * @param theString la traza a ser validada
     */
    
    public boolean validate(String theString) {
        String[] temp;
        String delimiter = "\\|";
        temp = theString.split(delimiter);
        boolean valid=true;
        if (temp.length != 9) {
            //System.out.print("overall");
            return false;
        }
        for (int i = 0; i < temp.length; i++) {
            if (temp[i].equals("")) {
                //System.out.print("null");
                valid=false;
            }
        }

        if (!temp[0].matches("CC|CE")) {//DOCUMENT TYPE
            //System.out.print("0");
                valid=false;
        }
        if (!temp[1].matches("^\\d{4,10}$")) { //NO DOCUMENT
            //System.out.print(temp[1]);
                valid=false;
        }
        if (!temp[2].matches("^[a-zA-Z ]+$")) {// NAME TITULAR
            //System.out.print(temp[2]);
            return false;
        }
        if (!temp[3].matches("CREDITO|DEBITO")) { //TARJET TYPE
            //System.out.print(temp[3]);
                valid=false;
        }
        if (!temp[4].matches("^\\d{0,16}$")) {// TARJET NUMBER
            //System.out.print(temp[4]);
                valid=false;
        }
        if (!temp[5].matches("AMEX|VISA|MC")) {//FRANC
            //System.out.print(temp[5]);
                valid=false;
        }
        if (!temp[6].matches("BANCOLOMBIA|DAVIVIENDA|MC")) { //BANK
            //System.out.print(temp[6]);
                valid=false;
        }
        if (!temp[7].matches("^\\d+$")) {//MOUNT
            //System.out.print(temp[7]);
            return false;
        }
        if (!temp[8].matches("^[a-zA-Z ]+$")) { //DESCRIPTION
            //System.out.print(temp[8]);
                valid=false;
        }
        
        Date date = new Date(12);
        entityArchive = new Archive(1, 2, 23, 5, 5, "C:/asf", date, date, 10, 2);
        long num=1;

        entityDocumentType = new DocumentType(num,"CC");
        documentTypeDAO= new DocumentTypeDAO();
        DocumentType documentType = new DocumentType();
        documentType = documentTypeDAO.searchByDescription(temp[0]);
        
        entityFranchise = new Franchise(num, "AMEX");
        franchiseDAO= new FranchiseDAO();
        Franchise franchise = new Franchise();
        franchise = franchiseDAO.searchByDescription(temp[5]);
                
        entityTraceState = new TraceState(num, "Todo Bien", "Válido");
        TraceState traceState = new TraceState();
        traceStateDao = new TraceStateDAO();
        traceState = traceStateDao.searchByTraceState("Todo Bien");
        
        
        /*
         * search all ID of diferent entity
         */
        
        
        entityTrace = new Trace(entityArchive, num, "CheckSum", traceState, franchise, documentType,12,Integer.valueOf(temp[1]), temp[2], temp[3], Long.valueOf(temp[4]), Long.valueOf(temp[7]), temp[8]);
        facadeClass facade = new facadeClass();
        facade.crear(entityTrace);
        
        return valid;
    }
}
